/*
 * DemoApp.cpp
 *
 *  Created on: 15 Oct 2013
 *      Author: Patrick
 */

#include "DemoApp.hpp"

//|||||||||||||||||||||||||||||||||||||||||||||||

DemoApp::DemoApp() {
	m_pOgreHeadNode = 0;
	m_pOgreHeadEntity = 0;

	m_bShutdown = false;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

DemoApp::~DemoApp() {
	delete OgreFramework::getSingletonPtr();
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void DemoApp::startDemo() {
	new OgreFramework();
	if (!OgreFramework::getSingletonPtr()->initOgre("DemoApp v1.0"))
		return;

	m_bShutdown = false;

	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Demo initialized!");

	OgreFramework::getSingletonPtr()->setUpDemoScene();
	runDemo();
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void DemoApp::runDemo() {
	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Start main loop...");

	double timeSinceLastFrame = 0;
	double startTime = 0;

	OgreFramework::getSingletonPtr()->m_pRenderWnd->resetStatistics();

	while (!m_bShutdown
			&& !OgreFramework::getSingletonPtr()->isOgreToBeShutDown()) {
		if (OgreFramework::getSingletonPtr()->m_pRenderWnd->isClosed())
			m_bShutdown = true;

		Ogre::WindowEventUtilities::messagePump();

		if (OgreFramework::getSingletonPtr()->m_pRenderWnd->isActive()) {
			startTime =
					OgreFramework::getSingletonPtr()->m_pTimer->getMillisecondsCPU();

			OgreFramework::getSingletonPtr()->captureInput();

			OgreFramework::getSingletonPtr()->updateOgre(timeSinceLastFrame);
			OgreFramework::getSingletonPtr()->m_pRoot->renderOneFrame();

			timeSinceLastFrame =
					OgreFramework::getSingletonPtr()->m_pTimer->getMillisecondsCPU()
							- startTime;
		} else {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
			Sleep(1000);
#else
			sleep(1);
#endif
		}
	}

	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Main loop quit");
	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Shutdown OGRE...");
}
